package puzzle.nurikabe.solver;

import java.util.List;
import java.util.Map;

import puzzle.common.data.HatanException;
import puzzle.common.point.Pnt;
import puzzle.common.solver.TopologicalRule;
import puzzle.nurikabe.data.NurikabeBoard;

class KabeEscapeRule implements TopologicalRule<NurikabeBoard, NurikabeTopology> {

	public boolean apply(NurikabeBoard b, NurikabeTopology topology) throws HatanException {
		if (topology.getKabeEscapePoints().size() == 1) {
			return false;
		}

		boolean isChanged = false;
		for (Map.Entry<Integer, List<Pnt>> entry : topology.getKabeEscapePoints().entrySet()) {
			List<Pnt> ps = entry.getValue();
			if (ps.size() == 0) {
				throw new HatanException();
			} else if (ps.size() == 1) {
				Pnt p = ps.get(0);
				isChanged |= b.setKabe(p.x, p.y);
			}
		}
		return isChanged;
	}

}
